<template>
	<!-- <uni-section :title="title" type="line"> -->
	<view class="example">
		<!-- <yile-breadcrumb :nav="nav" separator="/" size="16"></yile-breadcrumb> -->

		<!-- 基础表单校验 -->
		<!-- <uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData">


			<uni-forms-item label-width="150rpx" label="手机号" required name="mobile">
				<uni-easyinput v-model="valiFormData.mobile" placeholder="请输入手机号" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="姓名" required name="username">
				<uni-easyinput v-model="valiFormData.username" placeholder="请输入姓名" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="短码" name="short">
				<uni-easyinput v-model="valiFormData.short" placeholder="请输入短码" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="座机" name="telphone">
				<uni-easyinput v-model="valiFormData.telphone" placeholder="请输入座机" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="部门" required name="d_ids">
				<uni-data-select placeholder="请选择" :arrayConfig="configParams" :localdata="d_ids" v-model="dvalue"
					@change="d_change" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="职务" required name="j_ids">
				<uni-data-select placeholder="请选择" :arrayConfig="configParams" :localdata="j_ids" v-model="jvalue"
					@change="j_change" />
			</uni-forms-item>

			<uni-forms-item label-width="150rpx" label="备注" name="comment">
				<uni-easyinput v-model="valiFormData.comment" placeholder="请输入备注" />
			</uni-forms-item>

		</uni-forms> -->

		<button type="primary" @click="submit('valiForm')">提交</button>
	</view>
	<!-- </uni-section> -->
</template>

<script>
	import whtMd5 from '@/uni_modules/wht-md5/js_sdk/wht-md5.js'
	
	export default {
		data() {
			return {
				_id: '',
				dvalue: null,
				jvalue: null,
				d_ids: [],
				j_ids: [],
				type: '',
				nav: [{
					value: '首页', //名称
					url: '/pages/index/index', //链接
					type: 'switchTab', //跳转方式					
				}, {
					value: '用户列表', //名称
					url: 'list', //链接
					type: 'reLaunch', //跳转方式					
				}, {
					value: '新增用户',
					url: '',
					type: 'reLaunch',
					isActive: true //选中状态
				}],
				configParams: {
					labelKey: "name",
					valueKey: "_id",
				},
				valiFormData: {
					mobile: '',
					username: '',
					comment: '',
					short: '',
					telphone: '',
					d_ids: [],
					j_ids: [],
					status: 0,
					password: this.encrypt('111111'),
					role: {
						role_id: 0,
						role_name: '普通用户'
					},
					register_date: ''
				},
				rules: {
					mobile: {
						rules: [{
							required: true,
							errorMessage: '手机号不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的手机号'
						}, ]
					},
					username: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}, {
							minLength: 2,
							errorMessage: '姓名必须2个字以上'
						}, ]
					},
					d_ids: {
						rules: [{
							required: true,
							errorMessage: '请选择一个选项'
						}]
					},
					j_ids: {
						rules: [{
							required: true,
							errorMessage: '请选择一个选项'
						}]
					}
				},
			}
		},
		mounted() {
			this.getdep()
			this.getjob()		
		},

		methods: {
			encrypt(pwd){
				const hash32 = whtMd5(pwd)
				return hash32
			},
			
			async getuser() {
				const obj = uniCloud.importObject('users')
				const res = await obj.getUserInfobyId({
					_id: this._id
				}).then(res => {
					if (res.code == 0) {
						this.dataforeach(this.valiFormData, res.data)
						// //console.log('geetuser over')
					} else {
						uni.showToast({
							title: '数据异常',
							icon: 'error'
						})
					}
				})
			},
			dataforeach(data, res) {
				// //console.log(res, "res")
				Object.keys(data).forEach(key => {

					if (res[key]) {
						this.valiFormData[key] = res[key] || ''
					}
				})
				// //console.log(this.valiFormData)
			},
			async getdep() {
				const obj = uniCloud.importObject('basedata')
				let event = {
					data: {},
					field: {
						'name': true
					}
				}
				const res = await obj.getdepselect(event).then(res => {
					if (res.code == 0) {
						this.d_ids = res.data
						// //console.log('geetdep over')
					} else {
						uni.showToast({
							title: '部门数据异常',
							icon: 'fail'
						})
					}
				})
			},
			async getjob() {
				const obj = uniCloud.importObject('basedata')
				let event = {
					data: {},
					field: {
						'name': true
					}
				}
				const res = await obj.getjobselect(event).then(res => {
					if (res.code == 0) {
						this.j_ids = res.data
						// //console.log('geetjob over')
					} else {
						uni.showToast({
							title: '职务数据异常',
							icon: 'fail'
						})
					}
				})
			},
			d_change(e) {
				// //console.log('选择的值：', e);

				if (e._id) {
					this.dvalue = e._id
					this.valiFormData.d_ids = e
					// //console.log('dvalue的值：', this.dvalue);
				} else {
					this.dvalue = null
					this.valiFormData.d_ids = null
				}

			},
			j_change(e) {
				// //console.log('选择的值：', e);
				this.valiFormData.j_ids = e
				if (e._id) {
					this.jvalue = e._id
					this.valiFormData.j_ids = e
					// //console.log('jvalue的值：', this.jvalue);
				} else {
					this.jvalue = null
					this.valiFormData.j_ids = null
				}
			},
			submit(ref) {
				return
				this.$refs[ref].validate().then(res => {
					this.valiFormData.register_date = uni.$dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss')
					// // //console.log('验证通过11', res);
					this.valiFormData.mobile = Number(this.valiFormData.mobile)
					this.valiFormData.short = Number(this.valiFormData.short)
					this.valiFormData.telphone = Number(this.valiFormData.telphone)
					// // //console.log(this.valiFormData)
					const obj = uniCloud.importObject('users')
					const d = obj.register(this.valiFormData).then(res => {
						if (res.code == 0) {
							uni.showToast({
								title: res.message,
								icon: 'success'
							})
						} else {
							uni.showToast({
								title: res.message,
								icon: 'none'
							})
						}
					})
				}).catch(err => {
					//console.error('完整验证错误信息:', err) // 打印完整的错误对象
					// 新增：检查所有字段的验证状态
					// //console.log('当前表单数据:', JSON.stringify(this.valiFormData))
				})
			},
		}
	}
</script>

<style lang="scss">
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.form-item-label {
		width: 100rpx;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}

	.label-text {
		margin-top: 50rpx;
		line-height: 1.4;
		vertical-align: center;
	}

	.breadcrumb {
		margin-top: 50rpx;
		// margin-bottom: 50rpx;
	}
</style>